<template>
  <div class="box">
    <!--搜索与重置-->
    <el-form ref="form" label-width="100px" :model="query">
      <el-row>
        <el-col :md="8" :xl="6">
          <el-form-item label="姓名：" prop="name">
            <el-input v-model="query.name" placeholder="请输入姓名" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :md="8" :xl="6">
          <el-form-item label="部门：" prop="dept">
            <el-select v-model="query.dept" placeholder="请选择部门" class="full-width" clearable>
              <el-option v-for="item in deptList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :md="8" :xl="6">
          <el-form-item label="职位：" prop="position">
            <el-select v-model="query.type" placeholder="请选择职位" class="full-width" clearable>
              <el-option v-for="item in deptList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :md="8" :xl="6">
          <el-form-item label="角色：" prop="role">
            <el-select v-model="query.role" placeholder="请选择角色" class="full-width" clearable>
              <el-option v-for="item in deptList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :md="14" :xl="8">
          <el-form-item label-width="20px">
            <el-button type="primary" icon="el-icon-search" @click="handleCurrentChange(1)">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetSearch">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!--table-->
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      element-loading-text="加载中..."
      element-loading-spinner="el-icon-loading"
      class="table"
      stripe
      header-cell-class-name="table-header">
      <el-table-column label="序号" fixed type="index" align="center" width="55"></el-table-column>
      <el-table-column prop="name" label="姓名" min-width="120"></el-table-column>
      <el-table-column prop="dept" label="部门" min-width="150"></el-table-column>
      <el-table-column prop="position" label="职位" min-width="150"></el-table-column>
      <el-table-column prop="role" label="权限" min-width="180"></el-table-column>
      <el-table-column prop="email" label="邮箱" min-width="160"></el-table-column>
      <el-table-column prop="status" label="状态" width="80">
        <template v-slot="scope">
          <el-tag :type="scope.row.status ? 'success' : 'danger'">{{ scope.row.status ? '启用' : '停用' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" min-width="200"></el-table-column>
      <el-table-column label="操作" align="center" width="160" fixed="right">
        <template v-slot="scope">
          <el-button type="primary" size="mini" @click="editUser(scope.row.id)">编辑</el-button>
          <el-button type="danger" size="mini" @click="delUser(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      background
      :current-page="query.pageNo"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="query.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  name: 'User',
  data() {
    return {
      query: {
        name: '',
        dept: '',
        position: '',
        role: '',
        pageNo: 1,
        pageSize: 10
      },
      total: 0,
      tableData: [],
      loading: false,
      deptList: [
        { id: '1', name: '管理部' },
        { id: '2', name: '开发部' },
        { id: '3', name: '测试部' },
        { id: '4', name: '运维部' }
      ]
    }
  },
  created() {
    this.getTableData()
  },
  methods: {
    // 每页 val 条
    handleSizeChange(val) {
      this.query.pageNo = 1
      this.query.pageSize = val
      this.getTableData()
    },
    // 当前页: val
    handleCurrentChange(val) {
      this.query.pageNo = val
      this.getTableData()
    },
    // 重置
    resetSearch() {
      this.$refs.form.resetFields()
      this.query.pageNo = 1
      this.query.pageSize = 10
      this.getTableData()
    },
    // 获取用户信息
    getTableData() {
      this.loading = true
      setTimeout(() => {
        this.tableData = [
          {
            id: '1001',
            name: '张三',
            dept: '管理部',
            position: '经理',
            role: '系统管理员',
            email: '123456@163.com',
            status: 1,
            remark: ''
          },
          {
            id: '1002',
            name: '李四',
            dept: '开发部',
            position: 'web前端工程师',
            role: '普通角色',
            email: '123456@126.com',
            status: 1,
            remark: '主要负责后台管理系统以及H5端的开发'
          },
          {
            id: '1003',
            name: '王五',
            dept: '开发部',
            position: 'java工程师',
            role: '普通角色',
            email: '123456@qq.com',
            status: 1,
            remark: '负责后台任务的开发'
          },
          {
            id: '1003',
            name: '王五',
            dept: '测试部',
            position: '测试工程师',
            role: '普通角色',
            email: '123456@qq.com',
            status: 1,
            remark: '负责项目质量的把控'
          },
          {
            id: '1003',
            name: '王五',
            dept: '运维部',
            position: '运维工程师',
            role: '普通角色',
            email: '123456@qq.com',
            status: 0,
            remark: '负责公司服务器的运行维护'
          }
        ]
        this.total = 5
        this.loading = false
      }, 300)
    },
    // 修改用户信息
    editUser(id) {
      console.log('分配角色的用户id：', id)
    },
    // 删除用户
    delUser(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error',
        center: true
      }).then(() => {
        console.log('要删除的id：', id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
      })
    }
  }
}
</script>

<style scoped>

</style>
